package com.xca.is.model;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.xca.is.entity.YellowDate;
import com.xca.is.http.HttpConnect;
import com.xca.is.repository.YellowDateRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class YellowDateModel extends ViewModel {
    private YellowDateRepository repository ;
    private MutableLiveData<YellowDate> liveData = new MutableLiveData<>();
    private HttpConnect httpConnect ;
    public YellowDateModel(){
        repository = null ;
    }
    public void setModel(HttpConnect httpConnect){
        this.httpConnect = httpConnect ;
        repository = new YellowDateRepository();
    }

    public void getData(String date){
        repository.getData(date)
                .subscribe(new Observer<YellowDate>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        httpConnect.Loading(true);
                        Log.d("数据获取", "数据获取中");
                    }

                    @Override
                    public void onNext(@NonNull YellowDate yellowDate) {
                        if (yellowDate != null){
                            liveData.postValue(yellowDate);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("数据获取", "数据获取失败" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        httpConnect.Loading(false);
                        Log.d("数据获取", "数据获取成功");
                    }
                });
    }
    public MutableLiveData<YellowDate> getLiveData(){
        return liveData ;
    }
}
